//
//  WBTabbarController.m
//  WeatherBar
//
//  Created by yby on 2024/6/7.
//

#import "WBTabbarController.h"
#import "WBNavigationController.h"
#import "UIImage+WBImage.h"
#import "WBTabbar.h"

@interface WBTabbarController () <UITabBarControllerDelegate>

@end

@implementation WBTabbarController


static WBTabbarController *var_instance;
+ (instancetype)wb_shareInstance
{
    if (var_instance==nil) {
        var_instance = [[super allocWithZone:NULL] init];
    }
    return var_instance;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
    return [WBTabbarController wb_shareInstance];
}

- (id)copyWithZone:(struct _NSZone *)zone {
    return [WBTabbarController wb_shareInstance];
}
+ (void)wb_destroyInstance
{
//    NSLog(@"tabbarVC销毁了---");
    var_instance = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.delegate = self;
    
    [self wb_setCustomtabbar];
    
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *var_appearance = self.tabBar.standardAppearance;
        var_appearance.backgroundImage = [UIImage wb_imageWithColor:[UIColor color8744DF]];
        var_appearance.shadowImage = [UIImage wb_imageWithColor:[UIColor color8744DF]];
        self.tabBar.standardAppearance = var_appearance;
        
        var_appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffsetMake(0, -3);
        var_appearance.stackedLayoutAppearance.selected.titlePositionAdjustment = UIOffsetMake(0, -3);
        
        self.tabBarItem.standardAppearance = var_appearance;
    } else {
        self.tabBar.backgroundImage = [UIImage wb_imageWithColor:[UIColor whiteColor]];
        self.tabBar.shadowImage = [UIImage wb_imageWithColor:[UIColor clearColor]];
    }
}

- (void)wb_setCustomtabbar{

    WBTabbar *var_tabbar = [[WBTabbar alloc]init];
    
    [self setValue:var_tabbar forKeyPath:@"tabBar"];
    //添加点击手势
    UITapGestureRecognizer *var_click = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wb_centerBtnClick:)];
    
    [var_tabbar.var_centerBtn addGestureRecognizer:var_click];
    
    [self wb_configTabarbackgroundImage];
}

- (void)wb_configTabarbackgroundImage {
    [self.tabBar setShadowImage:[UIImage new]];
    if (@available(iOS 13.0, *)) {
        UIImage *var_backgroundImage = [UIImage imageNamed:@"tabbar_bg"];
        
        UIImageView *var_imageView = [[UIImageView alloc] initWithImage:var_backgroundImage];
        var_imageView.frame = CGRectMake(0, -22, UIScreen.mainScreen.bounds.size.width, self.tabBar.bounds.size.height);
        [self.tabBar insertSubview:var_imageView atIndex:0];

    }
}

- (void)wb_centerBtnClick:(UIButton *)btn{
    if (self.var_centerBtnClickBlock) {
        self.var_centerBtnClickBlock(YES);
    }
}

-(UIViewController *)wb_getCurrentVC
{
    UIViewController *var_rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController *var_currentVC = [self wb_getCurrentVCFrom:var_rootViewController];
    
    return var_currentVC;
}

-(UIViewController *)wb_getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *var_currentVC;
    
    if ([rootVC presentedViewController]) {
        rootVC = [rootVC presentedViewController];
    }
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        var_currentVC = [self wb_getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        var_currentVC = [self wb_getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
    } else {
        var_currentVC = rootVC;
    }
    return var_currentVC;
}
- (void)wb_tabbarAddChildController:(UIViewController*)childController
                           title:(NSString*)title
                       imageName:(NSString*)imageName
               selectedImageName:(NSString*)selectedImageName
         isRequiredNavController:(BOOL)isRequired
{
    childController.title = title;
    childController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 设置一下选中tabbar文字颜色color6061DC\color222
    [childController.tabBarItem setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor colorFFE300],
                                                          NSFontAttributeName:[UIFont systemFontOfSize:12.f weight:UIFontWeightMedium]
                                                       }forState:UIControlStateSelected];
    //#colorB0B0BF\colorCCC
    [childController.tabBarItem setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor colorC5C5C5],
                                                          NSFontAttributeName:[UIFont systemFontOfSize:12.f weight:UIFontWeightMedium] }forState:UIControlStateNormal];
//    childController.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -4);
//    childController.tabBarItem.imageInsets = UIEdgeInsetsMake(-4, 0, 0, 0);
    if (isRequired) {
        WBNavigationController* var_nav = [[WBNavigationController alloc] initWithRootViewController:childController];
        [self addChildViewController:var_nav];
        //        if (iPhoneX) {
        //            [nav.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -25)];
        //            nav.tabBarItem.imageInsets = UIEdgeInsetsMake(-8, 0, 5,0);
        //        }
    }else{
        [self addChildViewController:childController];
    }
}

- (UIImage *)wb_imageWithColor:(UIColor *)color{
    // 一个像素
    CGRect var_rect = CGRectMake(0, 0, 1, 1);
    // 开启上下文
    UIGraphicsBeginImageContextWithOptions(var_rect.size, NO, 0);
    [color setFill];
    UIRectFill(var_rect);
    UIImage *var_image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return var_image;
}

#pragma mark - UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
//    if ([WBUserManager shareManager].hasLogined) {
//        // 如果登录直接返回YES
//        return YES;
//    }
    return YES;
}

@end
